

import Taro from '@tarojs/taro'

const mjSubmitURL = `/mj/trigger/submit`;
const mjTaskQueryURL = `/ai/mj/task/result`;

const isDev = true;
// const isDev = false;
export const host = isDev ? 'http://192.168.1.12:8080' : 'https://wx.axises.fun'
export const wsHost = isDev ? 'ws://localhost:80' : 'wss://wx.axises.fun'

interface CallbackResp {
    success: (resp: any) => void;
    error?: (error: Error) => void;
}

export const sendGet = (url: string, data: any, callback: CallbackResp) => {
    try {
        Taro.request({
            url: url,
            method: "GET",
            data: JSON.stringify(data),
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: (res) => {
                console.log('sendGet消息请求成功', res, '参数:', data)
                callback.success(res)
            },
            fail: (e) => {
                console.log(url + ', sendGet消息请求失败:', e)
                callback.error && callback.error(new Error(e.errMsg))
            }
        })
    } catch (error) {
        callback.error && callback.error(new Error('请求结果异常'))
    }
}

export const sendPost = (url: string, data: any, callback: CallbackResp) => {
    try {
        Taro.request({
            url: url,
            method: "POST",
            data: JSON.stringify(data),
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: (res) => {
                console.log('sendPost消息请求成功', res, '参数:', data)
                callback.success(res)
            },
            fail: (e) => {
                console.log(url + ', sendPost消息请求失败:', e)
                callback.error && callback.error(new Error(e.errMsg))
            }
        })
    } catch (error) {
        callback.error && callback.error(new Error('请求结果异常'))
    }
}

export const SendMjSubmit = (message: string, callback: CallbackResp) => {
    // test
    // callback.success('test')

    try {
        Taro.request({
            url: host + mjSubmitURL,
            method: "POST",
            data: {
                "action": "IMAGINE",
                "prompt": message,
                "taskId": "1320098173412546",
                "index": 3,
                "state": "test:22"
            },
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: (res) => {
                console.log('mj消息请求成功', res)
                callback.success(res)
            },
            fail: (e) => {
                console.log('mj消息请求失败:', e)
                callback.error && callback.error(new Error(e.errMsg))
            }
        })
    } catch (error) {
        callback.error && callback.error(new Error('请求结果异常'))
    }
}

export const SendMjMessage = (messages: string[], callback: (msg, imgMsg) => void = () => { }) => {
    // test
    // getMjImageURLByTaskId(taskId, callback)

    Taro.request({
        url: host + mjSubmitURL,
        method: "POST",
        data: {
            "action": "IMAGINE",
            "prompt": messages[messages.length - 1],
            "taskId": "1320098173412546",
            "index": 3,
            "state": "test:22"
        },
        header: {
            'content-type': 'application/json' // 默认值
        },
        success: (res) => {
            console.log('mj消息请求成功', res)
            let taskId = res.data.result;
            // 触发任务获取图片
            getMjImageURLByTaskId(taskId, callback)
        },
        fail: (e) => {
            console.log('mj消息请求失败:', e)
            callback && callback('', '')
        }
    })
}

function getMjImageURLByTaskId(taskId: string, callback: (msg, imgMsg?: string) => void = () => { }) {
    console.log('查询任务信息, taskId:', taskId)

    // mock
    // callback('', "http://ys-aigc.oss-cn-beijing.aliyuncs.com/mj/dG52no.png");
    // return

    Taro.request({
        url: host + mjTaskQueryURL,
        method: "POST",
        data: {
            "taskId": taskId,
        },
        timeout: 3000,
        header: {
            'content-type': 'application/json' // 默认值
        },
        success: (res) => {
            console.log('获取任务信息成功:', res);
            if (res.statusCode === 504) {
                callback('图片处理超时', '')
            }
            if (res.data.data !== null) {
                callback('', res.data.data.imageURL);
            } else {
                callback(res.data.errorMsg, '')
            }
        },
        fail: (e) => {
            console.log('获取任务信息失败:', e);
            callback && callback(e.errMsg, '')
        }
    })
}

export default SendMjMessage;